﻿using System;
using UnityEngine;
using UnityEngine.UIElements;

namespace winS.UnityEditor.UIElement
{
    public class ToolView : VisualElement
    {
        public event Action<int> onIndexChanged;

        public int index { get => _index; set => toolbar[_index = value].Activate(); }
        public float barHeight { get => toolbar.height; set => toolbar.height = value; }

        public Toolbar toolbar { get; }
        public override VisualElement contentContainer => horizontalSlideView;

        private readonly HorizontalSlideView horizontalSlideView;

        private int _index;

        internal ToolView()
        {
            style.flexGrow = 1f;
            hierarchy.Add(toolbar = new Toolbar());
            hierarchy.Add(horizontalSlideView = new HorizontalSlideView());
            toolbar.onIndexChanged += index => onIndexChanged?.Invoke(index);
            horizontalSlideView.style.flexGrow = 1f;
            horizontalSlideView.style.marginTop = 2f;
        }
      
        public ToolbarButton CreatePage(string name, VisualElement page)
        {
            horizontalSlideView.Add(page);
            return toolbar.CreateToolBarButton(name, index => horizontalSlideView.SlideContent(_index = index));
        }
        public ToolbarButton CreatePage(Texture texture, string name, VisualElement page)
        {
            horizontalSlideView.Add(page);
            return toolbar.CreateToolBarButton(texture, name, index => horizontalSlideView.SlideContent(_index = index));
        }
    }
}